package cn.zhaoyuening.zshop4j.manager.service.impl;

import cn.zhaoyuening.zshop4j.common.bean.EasyUiTreeNode;
import cn.zhaoyuening.zshop4j.manager.mapper.TbItemCatMapper;
import cn.zhaoyuening.zshop4j.manager.pojo.TbItemCat;
import cn.zhaoyuening.zshop4j.manager.pojo.TbItemCatExample;
import cn.zhaoyuening.zshop4j.manager.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zhao on 2017/4/7.
 */
@Service("itemCatService")
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<EasyUiTreeNode> findByParentId(long id) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbItemCat> itemCatsList = itemCatMapper.selectByExample(example);

        //转换为 EasyUiTreeNode list
        List<EasyUiTreeNode> easyUiTreeNodeList = itemCatList2EasyUiTreeNodeList(itemCatsList);
        return easyUiTreeNodeList;
    }

    private List<EasyUiTreeNode> itemCatList2EasyUiTreeNodeList(List<TbItemCat> itemCatList) {
        List<EasyUiTreeNode> easyUiTreeNodeList = new ArrayList<>();
        for (TbItemCat itemCat :
                itemCatList) {
            EasyUiTreeNode node = new EasyUiTreeNode();
            node.setId(itemCat.getId());
            node.setState(itemCat.getIsParent());
            node.setText(itemCat.getName());
            easyUiTreeNodeList.add(node);
        }
        return easyUiTreeNodeList;
    }
}
